package tau.pimpmyfreedb.GUI;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import tau.pimpmyfreedb.common.IProgressUpdateClient;

public class WaitWindow implements IProgressUpdateClient {
    private Shell parent;
    private Shell shell;
    private Label label;
    private Label progressLabel;
    private Button btn;
    private boolean cancelButtonPressed;
    boolean closed = false;

    public WaitWindow(boolean closeOnCancel) {
	this(null, closeOnCancel);
    }

    public WaitWindow(Shell parent, boolean closeOnCancel) {
	this.parent = parent;
	cancelButtonPressed = false;
	shell = CreateShell(parent, closeOnCancel);
    }

    private Shell CreateShell(Shell parent, final boolean closeOnCancel) {
	// the shell
	final Shell shell;
	if (parent != null)
	    shell = SWTUtil.getDialog(parent);
	else
	    shell = SWTUtil.getDialog();
	shell.setLocation(200, 200);
	shell.setSize(600, 300);
	shell.setText("Please wait");
	// layout
	GridLayout gl = new GridLayout(1, false);
	gl.marginWidth = 20;
	gl.marginHeight = 20;
	shell.setLayout(gl);

	// label
	label = new Label(shell, SWT.WRAP);
	GridData gridData = new GridData();
	GridData lblGridData = new GridData();
	gridData.grabExcessHorizontalSpace = true;
	gridData.horizontalAlignment = SWT.FILL;
	// gridData.widthHint = 400;
	// gridData.heightHint = 100;
	lblGridData.grabExcessHorizontalSpace = true;
	lblGridData.horizontalAlignment = SWT.FILL;
	lblGridData.widthHint = 550;
	lblGridData.heightHint = 100;
	label.setLayoutData(gridData);

	shell.addShellListener(new ShellAdapter() {
	    @Override
	    public void shellClosed(ShellEvent e) {
		if (!closeOnCancel)
		    e.doit = false;
	    }
	});

	// progressLabel
	progressLabel = new Label(shell, SWT.WRAP);
	progressLabel.setLayoutData(lblGridData);

	// button
	btn = new Button(shell, SWT.PUSH);
	btn.setText("Stop");
	gridData = new GridData();
	gridData.horizontalAlignment = SWT.CENTER;
	gridData.widthHint = 150;
	btn.setLayoutData(gridData);
	btn.addSelectionListener(new SelectionAdapter() {
	    @Override
	    public void widgetSelected(SelectionEvent e) {
		cancelButtonPressed = true;
		btn.setEnabled(false);

		btn.setText("Stoping...");
		if (closeOnCancel)
		    close();

	    }
	});

	// shell.pack();
	return shell;
    }

    public boolean open(String msg) {
	// boolean answer = false;

	label.setText(msg);
	if (parent != null)
	    parent.setEnabled(false);
	shell.pack();
	shell.open();
	shell.setFocus();

	return true;
    }

    public void close() {
	shell.dispose();
	if (parent != null) {
	    parent.setEnabled(true);
	    parent.setFocus();
	}
    }

    // --- IProgressUpdateClient --- //

    @Override
    public void handleProgressUpdate(int percentage, final String message) {
	final String msg;
	if (percentage > -1) {
	    msg = String.format("%d%% complete - %s", percentage, message);
	} else {
	    msg = message;
	}
	System.out.println(msg);
	SWTUtil.getDisplay().asyncExec(new Runnable() {
	    @Override
	    public void run() {
		progressLabel.setText(msg);
	    }
	});
    }

    @Override
    public void handleOperationComplete() {
	if (!closed) {
	    SWTUtil.getDisplay().asyncExec(new Runnable() {
		@Override
		public void run() {
		    close();
		    closed = true;
		}
	    });
	}
    }

    @Override
    public boolean isCancelled() {
	return cancelButtonPressed;
    }
}
